package com.xinou.daily_report.controller;

import com.alibaba.excel.EasyExcel;
import com.xinou.common.base.config.ApiResponse;
import com.xinou.common.base.exception.BusException;
import com.xinou.common.enumerate.HttpStatusEnum;
import com.xinou.daily_report.PO.FruitPO;
import com.xinou.daily_report.PO.IntegralPO;
import com.xinou.daily_report.VO.fruit.FruitListVO;
import com.xinou.daily_report.VO.integral.IntegralCountVO;
import com.xinou.daily_report.VO.integral.IntegralExcelVO;
import com.xinou.daily_report.VO.integral.IntegralListVO;
import com.xinou.daily_report.VO.integral.IntegralVO;
import com.xinou.daily_report.mapper.SysIntegralMapper;
import com.xinou.daily_report.service.SysIntegralService;
import com.xinou.service.file.service.FileUploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-06-11
 */
@RestController
@RequestMapping("/admin/integral")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(tags = {"后台积分"})
public class SysIntegralController {
    private final SysIntegralService sysIntegralService;
    private final SysIntegralMapper sysIntegralMapper;
    private final FileUploadService fileUploadService;
    public static final String UploadFileUrl="https://image.xiaokesi.com/";

    @PostMapping("/list")
    @ApiOperation("获取积分列表")
    @RequiresPermissions({"admin:integral:list"})
    public ApiResponse<IntegralListVO> integralList(@RequestBody IntegralPO integralPO){
        return sysIntegralService.integralList(integralPO);
    }

    @PostMapping("/down")
    @ApiOperation("积分下载")
    @RequiresPermissions({"admin:integral:down"})
    public ApiResponse<String> integralDown(@RequestBody IntegralPO po) throws IOException {
        List<IntegralExcelVO> vos = sysIntegralMapper.integralDown(po);
        File tempFile = File.createTempFile(String.valueOf(System.nanoTime()),".xlsx");
        EasyExcel.write(tempFile, IntegralExcelVO.class).sheet("积分记录").doWrite(vos);
        String s = fileUploadService.fileUpload(tempFile).orElseThrow(() -> new BusException(HttpStatusEnum.RE_CODE_PARAM_ERROR, "下载积分记录失败"));
        return new ApiResponse<>(UploadFileUrl + s);
    }

    @PostMapping("/count")
    @ApiOperation("获取积分统计")
    @RequiresPermissions({"admin:integral:count"})
    public ApiResponse<IntegralCountVO> integralCount(@RequestBody IntegralPO integralPO){
        return sysIntegralService.integralCount(integralPO);
    }

    @PostMapping("/save")
    @ApiOperation("添加积分")
    @RequiresPermissions({"admin:integral:save"})
    public ApiResponse<Void> integralSave(@RequestBody IntegralPO integralPO){
        return sysIntegralService.integralSave(integralPO);
    }

    @PostMapping("/getById")
    @ApiOperation("根据id查询积分")
    @RequiresPermissions({"admin:integral:getById"})
    public ApiResponse<IntegralVO> integralGetById(@RequestBody IntegralPO integralPO){
        return sysIntegralService.integralGetById(integralPO);
    }

}

